package io.renren.modules.sp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.SeasonFilter;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.constant.Constant;
import io.renren.common.utils.DateUtils;
import io.renren.modules.sp.dao.*;
import io.renren.modules.sp.dto.SpMatchDTO;
import io.renren.modules.sp.dto.SpPassAccountDTO;
import io.renren.modules.sp.entity.*;
import io.renren.modules.sp.service.SpPassAccountService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dao.SysDictDataDao;
import io.renren.modules.sys.entity.SysDictDataEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 直通名单
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2025-05-29
 */
@Service
public class SpPassAccountServiceImpl extends CrudServiceImpl<SpPassAccountDao, SpPassAccountEntity, SpPassAccountDTO> implements SpPassAccountService {

    @Autowired
    private SpUserDao  spUserDao;
    @Autowired
    private SpMatchDao  spMatchDao;
    @Autowired
    private SpMatchItemDao spMatchItemDao;
    @Autowired
    private SpBaseMatchItemDao spBaseMatchItemDao;
    @Autowired
    private SpPassAccountDao spPassAccountDao;
    @Autowired
    private SysDictDataDao sysDictDataDao;


    @Override
    public QueryWrapper<SpPassAccountEntity> getWrapper(Map<String, Object> params){
        Object matchId = params.get("matchId");
        Object matchItemId = params.get("matchItemId");
        Object groupId = params.get("groupId");
        Object state = params.get("state");
        Object beginSignup = params.get("beginSignup");
        Object endSignup = params.get("endSignup");

        QueryWrapper<SpPassAccountEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(matchId!=null, "match_id", matchId);
        wrapper.eq(matchItemId!=null, "match_item_id", matchItemId);
        wrapper.eq(groupId!=null, "group_id", groupId);
        wrapper.eq(state!=null, "state", state);

        // 处理时间范围查询
        if (beginSignup != null && endSignup != null) {
            wrapper.between("use_time", beginSignup, endSignup);
        } else {
            wrapper.ge(beginSignup != null, "use_time", beginSignup)
                    .le(endSignup != null, "use_time", endSignup);
        }


        return wrapper;
    }

    @Override
    public PageData<SpPassAccountEntity> queryPage(Map<String, Object> params) {
        Page page = createPage(params);
        //分页参数
        params.put(Constant.PAGE, page);
        IPage<SpPassAccountEntity> page2 = spPassAccountDao.selectPageVo(page, params);
        return getPageData(page2, SpPassAccountEntity.class);
    }

    @Override
    @Transactional
    public boolean insertBatch(Collection<SpPassAccountEntity> list) {
        List<SpPassAccountEntity> entityList = (List<SpPassAccountEntity>) list;
        /**
         姓名错误
         账号错误
         赛事错误
         赛项错误
         直通码错误
         该选手已经报名该赛项
         赛项互斥的逻辑
         */
        for (SpPassAccountEntity entity : entityList) {
            //根据姓名和账号去验证
            List<SpUserEntity> userList = spUserDao.selectList(
                    new QueryWrapper<SpUserEntity>()
                            .eq("real_name", entity.getRealName())
                            .eq("account", entity.getAccount())
            );
            if(userList.size()==0){
                entity.setMsg("姓名或账号错误");
            }
            //验证赛事名是否存在
            List<SpMatchEntity> matchList = spMatchDao.selectList(
                    new QueryWrapper<SpMatchEntity>()
                            .eq("match_name", entity.getMatchName())
            );
            if(matchList.size()==0){
                entity.setMsg("赛事名错误");
            }
            //赛项错误
            List<SpBaseMatchItemEntity> itemList = spBaseMatchItemDao.selectList(
                    new QueryWrapper<SpBaseMatchItemEntity>()
                            .eq("item_name", entity.getMatchItemName())
            );
            if(itemList.size()==0){
                entity.setMsg("赛项名错误");
            }
            //判断赛事下是否有当前赛项
            List<SpMatchItemEntity> matchItemList = spMatchItemDao.selectList(
                    new QueryWrapper<SpMatchItemEntity>()
                    .eq("match_id", matchList.get(0).getId())
                    .eq("base_match_item_id", itemList.get(0).getId())
            );
            if(matchItemList.size()==0){
                entity.setMsg("赛事下没有此赛项");
            }
            //查询组别
            List<SysDictDataEntity> groupList = sysDictDataDao.selectList(
                    new QueryWrapper<SysDictDataEntity>()
                            .eq("dict_type_id", "100300")
                            .eq("dict_label", entity.getGroupName())
            );
            if(groupList.size()==0){
                entity.setMsg("组别错误");
            }
            entity.setGroupId(groupList.get(0).getId().intValue());


            entity.setMatchId(matchList.get(0).getId().intValue());
            entity.setMatchItemId(matchItemList.get(0).getId().intValue());

            //校验直通码是否重复
            List<SpPassAccountEntity> passAccountList = spPassAccountDao.selectList(
                    new QueryWrapper<SpPassAccountEntity>()
                            .eq("code", entity.getCode())
            );
            if(passAccountList.size()>0){
                entity.setMsg("此直通码已存在");
            }
        }

        System.out.println(entityList);
        if (entityList.stream().filter(entity -> StringUtils.isNotBlank(entity.getMsg())).count() > 0) {
            return false;
        }

        //循环插入数据库
        for (SpPassAccountEntity entity : entityList) {
            spPassAccountDao.insert(entity);
        }

        return true;
    }


}
